﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;
using NS.Utilities;
using NS.BLL.Systems;
using NS.Model.Systems;

public partial class Platform_DeptManager : System.Web.UI.Page
{
    public long ParentID = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["ParentID"] != null)
        {
            ParentID = Convert.ToInt32(Request.QueryString["ParentID"]);
        }

        if (!IsPostBack)
        {
            GridViewDataBind();
        }
    }
    private void GridViewDataBind()
    {
        FGridView1.SQLString = DeptService.GetListStr(ParentID);
        FGridView1.PKey = "DeptID";
        FGridView1.OrderName = "OrderID,DeptID";
        FGridView1.BindData();
        FGridView1.AddControl();
    }
    protected void btnBindData_Click(object sender, EventArgs e)
    {
        GridViewDataBind();
    }
    protected void LBAdd_Click(object sender, EventArgs e)
    {
        Dept Model = new Dept();
        Model.DeptName = txtDeptName.Text.Trim();
        Model.ParentID = ParentID;
        if (ParentID != 0)
        {
            object[] ParentInfo = UtilityService.GetFieldValueByPK("Dept", "ParentPath,Depth", "DeptID", ParentID.ToString());
            Model.ParentPath = ParentInfo[0].ToString() + "," + ParentID;
            Model.Depth = Convert.ToInt32(ParentInfo[1]) + 1;
        }
        else
        {
            Model.ParentPath = "0";
            Model.Depth = 0;
        }
        Model.OrderID = Convert.ToInt32(txtOrderID.Text.Trim());

        if (DeptService.Insert(Model) > 0)
        {
            txtDeptName.Text = string.Empty;
            txtOrderID.Text = string.Empty;
            GridViewDataBind();
            MessageBox.ShowSaveSuccess(this);
        }
        else
        {
            MessageBox.ShowSaveError(this);
        }
    }

    protected void DelButton_Click(object sender, EventArgs e)
    {
        string DelID = ((Button)sender).CommandArgument.ToString();
        DeptService.DeleteAll(Convert.ToInt64(DelID));
        GridViewDataBind();
    }
}